package com.leetcode;

/**
 * 3. 无重复字符的最长子串
 * 滑动窗口 + 数组记录使用状态
 */
public class Solution3_3 {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int l = 0;
        int r = -1;
        int length = 0;
        boolean[] isVisted = new boolean[256];
        while (l < n) {
            if (r + 1 < n && !isVisted[s.charAt(r + 1)]) {
                isVisted[s.charAt(++r)] = true;
            } else {
                isVisted[s.charAt(l++)] = false;
            }
            length = Math.max(length, r - l + 1);
        }
        return length;
    }

    public static void main(String[] args) {
        String s = "abcabcbb";
        int res = new Solution3().lengthOfLongestSubstring(s);
        System.out.println(res);
    }
}
